/**
 * 
 * @author Sunix
 * @date   2013-5-8
 * @Email  SunixSky@gmail.com
 * 
 * 
 * 动画播放器
 * 
 */	
package sunix.animationPlayer{
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.getTimer;
	
	import sunix.animationPlayer.data.FrameData;
	import sunix.animationPlayer.events.AnimationPlayerEvent;
	
	[Event(name="destory", type="sunix.animationPlayer.events.AnimationPlayerEvent")]
	public class AnimationPlayer extends Sprite{
		
		/**
		 * 播放器的名字 
		 */		
		public var playerName:String; 
		/**
		 * 是否激活 
		 */		
		public var active:Boolean = false; 
		/**
		 * 帧的总数 
		 */		
		public var totalFrames:uint = 0;
		
		protected var container:Sprite;    //动画容器
		protected var bimtmap:Bitmap;      //图片容器
		protected var rate:Number;         //速率
		protected var startTime:Number;    //开始播放时间
		protected var renderFrameNum:int = -1;  //渲染帧号
		protected var cycle:Boolean = true;    //循环播放标志，默认为循环播放
		
		private var _fps:uint;   //帧频
		private var _currentFrameNum:int=0;  //当前帧数
		private var _frames:Vector.<FrameData>;  //帧数据集合
		
		/**
		 * 构造函数 
		 * 
		 */		
		public function AnimationPlayer(){
			viewInit();
			eventInit();
		}
		
		/**
		 * 播放 
		 * 
		 * 默认为循环播放
		 * 
		 */		
		public function play(value:Boolean = true):void{
			cycle = value;
			active = true;
			start();
		}
		
		/**
		 * 停止 
		 * 
		 */		
		public function stop():void{
			active = false;
		}
		
		/**
		 * 从指定帧开始播放 
		 * 
		 */		
		public function gotoAndPlay(num:uint):void{
			currentFrameNum = num;
			play();
		}
		
		/**
		 * 停止到指定帧 
		 * 
		 */		
		public function gotoAndStop(num:uint):void{
			active = false;
			currentFrameNum = num;
			draw();
		}
		
		/**
		 * 前一帧 
		 * 
		 * 仅供手动操作
		 */		
		public function prevFrame():void{
			active = false;
			currentFrameNum--;
			draw();
		}
		
		/**
		 * 下一帧 
		 * 
		 * 仅供手动操作
		 */		
		public function nextFrame():void{
			active = false;
			currentFrameNum++;
			draw();
		}
		
		/**
		 * 渲染
		 * 
		 */		
		public function render():void{
			handleFrameNum();
			draw();
		}
		
		/**
		 * 释放方法 
		 * 
		 */		
		public function dispose():void{
			clear();
		}
		
		/**
		 * 清理 
		 * 
		 */		
		public function clear():void{
			active = false;
			bimtmap.bitmapData.dispose();
		}
		
		/**
		 * 帧数处理 
		 * 
		 * 判断当前是够该进行帧数增加 
		 */		
		protected function handleFrameNum():void{
			var now:Number = getTimer();
			var value:Number = now - startTime;
			if(value >= rate){   // TODO 是否需要严格的帧数补充
				startTime = getTimer();
				currentFrameNum ++;
			}
		}
		
		/**
		 * 绘制
		 *  
		 * 
		 */		
		protected function draw():void{
			if(renderFrameNum == currentFrameNum)
				return;
			var frameData:FrameData = frames[currentFrameNum];
			bimtmap.x = -frameData.offsetPoint.x;
			bimtmap.y = -frameData.offsetPoint.y;
			bimtmap.bitmapData = frameData.bitmapData;
			renderFrameNum = currentFrameNum;
		}
		
		/**
		 * 事件初始化 
		 * 
		 */		
		protected function eventInit():void{
			addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
			addEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromStage);
		}
		
		/**
		 * 显示对象初始化 
		 * 
		 */		
		protected function viewInit():void{
			container = new Sprite();
			addChild(container);
			bimtmap = new Bitmap();
			bimtmap.smoothing = true;
			container.addChild(bimtmap);
		}
		
		protected function start():void{
			startTime = getTimer();
		}
		
		/**
		 * 事件移除
		 * 
		 */		
		protected function eventRemove():void{
			removeEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
			removeEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromStage);
		}
		
		/**
		 * 添加到舞台
		 *  
		 * @param event
		 * 
		 */		
		protected function onAddedToStage(event:Event):void{
			//TODO
		}
		
		/**
		 * 从舞台移除
		 *  
		 * @param event
		 * 
		 */		
		protected function onRemoveFromStage(event:Event):void{
			dispatchEvent(new AnimationPlayerEvent(AnimationPlayerEvent.DESTORY));
		}

//////////////////////////////////////////  getter/setter  //////////////////////////////////////////////////		
		/**
		 * 帧频
		 *  
		 * @return 
		 * 
		 */		
		public function get fps():uint{
			return _fps;
		}

		public function set fps(value:uint):void{
			_fps = value;
			rate = 1000 / _fps;
		}

		/**
		 * 当前帧号
		 *  
		 * @return 
		 * 
		 */		
		public function get currentFrameNum():int{
			return _currentFrameNum;
		}

		public function set currentFrameNum(value:int):void{
			_currentFrameNum = value;
			if(_currentFrameNum >= totalFrames){  //循环播放完毕后，重置
				_currentFrameNum = 0;
			}
			if(_currentFrameNum < 0){ 
				_currentFrameNum = totalFrames - 1;
			}
			if(!cycle){
				active = false;
				dispatchEvent(new AnimationPlayerEvent(AnimationPlayerEvent.COMPLETE));
			}
		}

		/**
		 * 当前播放的帧动画集合
		 *  
		 * @return 
		 * 
		 */		
		public function get frames():Vector.<FrameData>{
			return _frames;
		}

		public function set frames(value:Vector.<FrameData>):void{
			_frames = value;
			totalFrames = value.length;
		}
	}
}